package com.example.szh.mvp.view.user;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;
import com.example.szh.R;
import com.example.szh.action.BroadAction;
import com.example.szh.db.DBManager;
import com.example.szh.entity.BaseEntity;
import com.example.szh.entity.LoginEntity;
import com.example.szh.entity.UserEntity;
import com.example.szh.http.HttpCode;
import com.example.szh.mvp.contract.LoginContract;
import com.example.szh.mvp.di.DaggerLoginComponent;
import com.example.szh.mvp.di.LoginMoudes;
import com.example.szh.mvp.presenter.LoginPresenter;
import com.example.szh.nextwork.view.BaseActivity;
import com.example.szh.utils.ToastUtils;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

//import com.sina.weibo.sdk.auth.AccessTokenKeeper;
//import com.sina.weibo.sdk.auth.WbConnectErrorMessage;
//import com.sina.weibo.sdk.auth.sso.SsoHandler;

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.ILoginView,View.OnClickListener {

//    private SsoHandler mSsoHandler;
//    private Oauth2AccessToken mAccessToken;

    private RelativeLayout loginLinear;
    private ImageView logImg;
    private LinearLayout loginNamePwd;
    private LinearLayout loginPhone;
    private Button loginBnt;
    private TextView loginType;
    private EditText name;
    private EditText password;
    private ImageView pwdtype;
    private int PwdShow=0;
    private ImageView XLLogin;
    private SharedPreferences isLogin;

    @Override
    public View addStatusView() {
        return f(R.id.login_linear);
    }

    @Override
    public void refresh(BaseEntity baseEntity) {
        if (baseEntity.message.equals("用户登录成功")){
            LoginEntity loginEntity = (LoginEntity) baseEntity;
            Intent intent = new Intent();
            intent.setAction(BroadAction.UPDATAUSERACTION);
            intent.putExtra("userValues", loginEntity.getValues());
            sendBroadcast(intent);
            finish();
            SharedPreferences.Editor edit = isLogin.edit();
            edit.putBoolean("is", true);
            edit.commit();
            try {
                DBManager.getInstance().getDaoSession().deleteAll(UserEntity.class);
                DBManager.getInstance().getDaoSession().insert(loginEntity.getValues());
                LogUtils.json(DBManager.getInstance().getDaoSession().loadAll(UserEntity.class));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        loginLinear = (RelativeLayout) findViewById(R.id.login_linear);
        logImg = (ImageView) findViewById(R.id.log_img);
        loginNamePwd = (LinearLayout) findViewById(R.id.login_namePwd);
        loginPhone = (LinearLayout) findViewById(R.id.login_Phone);
        loginBnt = (Button) findViewById(R.id.login_bnt);
        loginType = (TextView) findViewById(R.id.loginType);
        name = (EditText) findViewById(R.id.name);
        password = (EditText) findViewById(R.id.password);
        pwdtype = (ImageView) findViewById(R.id.pwdtype);
        XLLogin = (ImageView) findViewById(R.id.XL_login);
        loginBnt.setOnClickListener(this);
        loginType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (loginType.getText().toString().equals("手机登录")){
                    AnimatorSet animatorSet=new AnimatorSet();
                    ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(loginNamePwd, "alpha",0,1);
                    ObjectAnimator objectAnimator1=ObjectAnimator.ofFloat(loginNamePwd, "translationY",0,200,0);
                    animatorSet.setDuration(2000);
                    animatorSet.playTogether(objectAnimator,objectAnimator1);
                    animatorSet.start();
                    loginNamePwd.setVisibility(View.VISIBLE);
                    loginPhone.setVisibility(View.INVISIBLE);
                    loginType.setText("账号登录");
                }else {
                    AnimatorSet animatorSet=new AnimatorSet();
                    ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(loginPhone, "alpha",0,1);
                    ObjectAnimator objectAnimator1=ObjectAnimator.ofFloat(loginPhone, "translationY",0,200,0);
                    animatorSet.setDuration(5000);
                    animatorSet.playTogether(objectAnimator,objectAnimator1);
                    animatorSet.start();
                    loginPhone.setVisibility(View.VISIBLE);
                    loginNamePwd.setVisibility(View.INVISIBLE);
                    loginType.setText("手机登录");
                }
            }
        });

        pwdtype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(PwdShow==0){
                    password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    pwdtype.setImageResource(R.mipmap.res_7icon_202);
                    PwdShow=1;
                }else {
                    password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    pwdtype.setImageResource(R.mipmap.res_7icon_201);
                    PwdShow=0;
                }
            }
        });
        XLLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UMShareAPI.get(LoginActivity.this).getPlatformInfo(LoginActivity.this,
                        SHARE_MEDIA.SINA,authListener);
//                mSsoHandler.authorizeClientSso(new SelfWbAuthListener());
            }
        });

    }

    UMAuthListener authListener = new UMAuthListener() {
        /**
         * @desc 授权开始的回调
         * @param platform 平台名称
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {
            Log.e("ZXY","onStart");
        }
        /**
         * @desc 授权成功的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         * @param data 用户资料返回
         */
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            Log.e("ZXY","onComplete"+data.get("name"));
        }
        /**
         * @desc 授权失败的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Log.e("ZXY","onError");
        }
        /**
         * @desc 授权取消的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         */
        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Log.e("ZXY","onCancel");
        }
    };



    


    @Override
    public void inject() {
        DaggerLoginComponent.builder().loginMoudes(new LoginMoudes(this)).build().inject(this);
    }

    @Override
    public void initData() {
//        mSsoHandler = new SsoHandler(LoginActivity.this);
        isLogin = getSharedPreferences("isLogin", MODE_PRIVATE);
        boolean is = isLogin.getBoolean("is", false);
        if (is){
            finish();
        }

    }

    @Override
    public void onClick(View v) {
        String trim = name.getText().toString().trim();
        Log.e("aaa", name.getText().toString().trim()+"  "+password.getText().toString().trim());
        Long aLong = Long.valueOf(trim);
        if (aLong.toString().length()!=9)
            ToastUtils.showMsg("手机号格式不正确");
        if (v.getId()==R.id.login_bnt){
            p.requestLogin(aLong,password.getText().toString().trim(), HttpCode.LOGINCODE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}